<?php

/**
 * Project: Vietsam
 * Package Controller
 * Class: Tab
 * Author: Khoi Pham		email: pmkhoi@gmail.com
 * Created on Dec 18, 2008
 * Updated on Dec 30, 2008
 *  
 * */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Provider extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('provider_model', 'provider', true);
        $this->load->helper('form');
        $this->checkLogin();
        $this->template->set_template('admin');
        $this->buildMenu();
        //$this->checkPermission();
    }

    function Provider() {
        parent::__construct();
        $this->load->library('xajax', 'profile');
        $this->xajax->registerFunction(array('test', &$this, 'test'));

        $this->xajax->processRequest();
        $this->xajax_js = $this->xajax->getJavascript($this->base_url . 'js/');
        $this->load->model('provider_model', 'provider', true);
        $this->load->helper('form');
        $this->main = '';
        $this->checkLogin();
        $this->checkPermission();
    }

    function index() {
        $this->view();
    }

    function create() {
        $data = array();
        $data['id_provider'] = 0;
        $data['vi_name'] = '';
        $data['en_name'] = '';
        $data['error_message'] = '';
        $data['legend_text'] = lang('create_provider');
        $this->template->parse_view('content', 'admin/provider/create_provider', $data);
        $this->template->render();
    }

    function edit($id_provider) {
        $provider = $this->provider->getById($id_provider);
        if (!$provider) {
            redirect('admin/provider');
            return;
        }
        
        $data = $provider;
        $data['error_message'] = '';
        $data['legend_text'] = lang('edit_provider');
        $this->template->parse_view('content', 'admin/provider/create_provider', $data);
        $this->template->render();
    }

    function save() {
        $id = (int) $this->input->post('txtID');
        $provider = new stdClass();
        $data = array();
        if ($id > 0) {
            $provider = $this->provider->getById($id);
            if (!$provider) {
                redirect('admin/provider');
                return;
            }

            $data = $provider;

        } else {
            $data['id_provider'] = 0;
        }

        $data['vi_name'] = trim($this->input->post('txtViName'));
        $data['en_name'] = trim($this->input->post('txtEnName'));
        $data['order_view'] = 0;
        $error = false;
        $error_message = '';

        $this->load->library('form_validation');

        $this->form_validation->set_rules('txtViName', 'lang:vi_name', 'required|min_length[3]|max_length[50]');
        $this->form_validation->set_rules('txtEnName', 'lang:en_name', 'required|min_length[3]|max_length[50]');

        if ($this->form_validation->run() == FALSE) {
            $data['error_message'] = '<div style="color:red">' . validation_errors() . '</div>';
            $data['legend_text'] = $id > 0 ? lang('edit_provider') : lang('create_provider');
            $this->template->parse_view('content', 'admin/provider/create_provider', $data);
            $this->template->render();
        } else {
            if ($id > 0) {
                $result = $this->provider->update($data);
            } else {
                $this->provider->vi_name = $data['vi_name'];
                $this->provider->en_name = $data['en_name'];
                $this->provider->order_view = $data['order_view'];
                $id_provider = $this->provider->insert();
            }

            redirect('admin/provider/');
        }
    }

    function view() {
        $NUMBER_RECORD_PER_PAGE = 15;
        $this->load->library('pagination');
        $page = (int) $this->uri->segment(4);
        $config['base_url'] = $this->base_url . 'admin/provider/view';
        $config['total_rows'] = $this->provider->getCountAllAdmin();
        $config['per_page'] = $NUMBER_RECORD_PER_PAGE;
        $config['uri_segment'] = 4;
        $config['first_link'] = 'First';

        $this->pagination->initialize($config);

        $rows = $this->provider->getAllAdmin($page, $NUMBER_RECORD_PER_PAGE);
        $data = array();
        $data['rows'] = $rows;

        $this->template->parse_view('content', 'admin/provider/providers_list', $data);
        $this->template->render();
    }

    function delete() {
        $id = intval($this->uri->segment(4));
        $ids = array();
        if ($id > 0) {
            $ids[] = (int) $id;
            //$this->product->delete($id);
        } else {
            if (($this->input->post('submitDelcategory'))) {
                $ids = $this->input->post('categoryBox');
            }
        }

        if (count($ids)) {

            $result = $this->provider->delete($ids);
        }

        redirect('admin/provider/');
    }

    function change_order() {
        $id1 = (int) $this->uri->segment(4);
        $id2 = (int) $this->uri->segment(5);

        $cat1 = $this->provider->getById($id1);
        $cat2 = $this->provider->getById($id2);

        if ($cat1 AND $cat2) {
            $position1 = $cat1->order_view;
            $cat1->order_view = $cat2->order_view;
            $cat2->order_view = $position1;

            $this->provider->update($cat1);
            $this->provider->update($cat2);
        }

        redirect($_SESSION['his_adm']);
    }

}

?>
